package com.breakfast.fun.util;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PointF;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.breakfast.fun.R;

/**
 * 购物车动画
 * Created by Administrator on 2017/1/16.
 */

public class CartAnimUtil {
    private Context mContext;
    private View fromView;          //起始点
    private View toView;            //目标点
    private static ViewGroup rootView;          //父布局

    public CartAnimUtil(Context context) {
        mContext = context;
    }

    public CartAnimUtil(Context context, View fromView, View toView, ViewGroup rootView) {
        mContext = context;
        this.fromView = fromView;
        this.toView = toView;
        this.rootView = rootView;
    }

    public void startAnim() {
        int[] itemCoordinate = new int[2];          //被点击item坐标
        int[] shopCoordinate = new int[2];          //购物车坐标
        fromView.getLocationInWindow(itemCoordinate);
        toView.getLocationInWindow(shopCoordinate);
        PointF control = new PointF(shopCoordinate[0], itemCoordinate[1]);
        PointF start = new PointF(itemCoordinate[0] + 130, itemCoordinate[1] - 20);
        PointF end = new PointF(shopCoordinate[0] - 5, shopCoordinate[1] - 10);
        ImageView iv = new ImageView(mContext);
        iv.setImageResource(R.drawable.ssdk_oks_classic_qzone);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                70,
                70);
        iv.setLayoutParams(lp);
        rootView.addView(iv);

        //实时改变ImageView 的值实现移动效果
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new BezierTypeEvaluator(control), start, end);
        valueAnimator.setInterpolator(new DecelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF point = (PointF) animation.getAnimatedValue();
                iv.setX(point.x);
                iv.setY(point.y);
            }
        });

        //购物车实现一个抖动效果的补间动画
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                rootView.removeView(iv);
                //shopImg 开始一个放大动画
                ScaleAnimation a = new ScaleAnimation(0.6f, 1.3f, 0.6f, 1.3f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                a.setFillAfter(false);
                a.setInterpolator(new DecelerateInterpolator());
                a.setDuration(200);
                toView.startAnimation(a);
            }
        });

        ValueAnimator rotateAnimator = ValueAnimator.ofInt(0, 360 * 7);
        rotateAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Integer i = (Integer) animation.getAnimatedValue();

                iv.setRotation((i.intValue()));
            }
        });
        rotateAnimator.setTarget(iv);
        rotateAnimator.setDuration(500);

        valueAnimator.setTarget(iv);
        valueAnimator.setDuration(500);
        valueAnimator.start();
        rotateAnimator.start();
    }

    /**
     * 自定义贝塞尔曲线插值器
     */
    private class BezierTypeEvaluator implements TypeEvaluator<PointF> {
        private PointF mPointF;

        public BezierTypeEvaluator(PointF mPointF) {
            super();
            this.mPointF = mPointF;
        }

        @Override
        public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
            return getBezierPoint(startValue, endValue, mPointF, fraction);
        }

        /**
         * 二次贝塞尔曲线公式
         *
         * @param start   开始的数据点
         * @param end     结束的数据点
         * @param control 控制点
         * @param t       float 0-1
         * @return 不同t对应的PointF
         */
        private PointF getBezierPoint(PointF start, PointF end, PointF control, float t) {
            PointF newPoint = new PointF();
            newPoint.x = (1 - t) * (1 - t) * start.x + 2 * t * (1 - t) * control.x + t * t * end.x;
            newPoint.y = (1 - t) * (1 - t) * start.y + 2 * t * (1 - t) * control.y + t * t * end.y;
            return newPoint;
        }
    }


    public void setFromView(View fromView) {
        this.fromView = fromView;
    }

    public void setToView(View toView) {
        this.toView = toView;
    }

    public void setRootView(ViewGroup rootView) {
        this.rootView = rootView;
    }

    public static ViewGroup getRootView() {
        return rootView;
    }
}
